package com.caow.baselibrary

import android.os.Build.VERSION
import android.os.Bundle
import androidx.annotation.ColorInt
import androidx.appcompat.app.AppCompatActivity
import androidx.core.graphics.ColorUtils
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.alibaba.android.arouter.launcher.ARouter
import com.caow.baselibrary.event.LogoutEvent
import com.caow.baselibrary.utils.UIUtils
import com.caow.baselibrary.widget.LoadingDialog
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import java.lang.reflect.ParameterizedType


/**
 * @Author caow
 * 2022/11/18
 */
abstract class BaseActivity<viewDataBinging : ViewDataBinding, VM : BaseViewModel> :
    AppCompatActivity() {

    protected val TAG by lazy { javaClass.simpleName }

    protected val binding: viewDataBinging by lazy {
        DataBindingUtil.setContentView<viewDataBinging>(
            this,
            this.initLayoutId()
        )
    }
    protected val viewModel: VM by lazy {
        val entityClass: Class<VM> =
            (this.javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[1] as Class<VM>
        ViewModelProvider(
            this,
            ViewModelProvider.AndroidViewModelFactory.getInstance(application)
        ).get(entityClass)
    }

    // 加载中弹窗
    private val loadingDialog by lazy { LoadingDialog(this) }

    abstract fun initLayoutId(): Int
    abstract fun initViewModelId(): Int

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initImmersiveUtils(isImmersive())
        ARouter.getInstance().inject(this)
        binding.setVariable(initViewModelId(), viewModel)
        lifecycle.addObserver(viewModel)
        EventBus.getDefault().register(this)
//        UIUtils.hideNavigation(window)
        initLoadingDialog()
    }

    override fun onDestroy() {
        EventBus.getDefault().unregister(this)
        super.onDestroy()
    }

    private fun initLoadingDialog() {
        viewModel.showLoadingDialog.observe(this, Observer {
            onLoadingShow()
            loadingDialog.show()
        })
        viewModel.dismissLoadingDialog.observe(this, Observer {
            loadingDialog.dismiss()
            onLoadingDismiss()
        })

        viewModel.closeActivity.observe(this, Observer { finish() })
    }

    protected open fun onLoadingDismiss() {}
    protected open fun onLoadingShow() {}

    protected open fun initImmersiveUtils(immersive: Boolean) {
        if (immersive) {
            if (VERSION.SDK_INT >= 21) {
                val decorView = this.window.decorView
                val option = 1280
                decorView.systemUiVisibility = option
                this.window.statusBarColor = 0
            }
            UIUtils.hideNavigation(window)
        } else {
//            this.setStatusHintDark(this.isLightColor(resources.getColor(R.color.colorPrimaryDark)))
        }
    }

    override fun isImmersive(): Boolean {
        return false
    }

    protected open fun isLoginPage(): Boolean {
        return false
    }

    private fun setStatusHintDark(bDark: Boolean) {
        if (VERSION.SDK_INT >= 23) {
            val decorView = this.window.decorView
            if (decorView != null) {
                var vis = decorView.systemUiVisibility
                vis = if (bDark) {
                    vis or 8192
                } else {
                    vis and -8193
                }
                decorView.systemUiVisibility = vis
            }
        }
    }

    private fun isLightColor(@ColorInt color: Int): Boolean {
        return ColorUtils.calculateLuminance(color) > 0.5
    }

    @Subscribe
    fun onEventLogout(event: LogoutEvent) {
        if (!isLoginPage()) {
            finish()
        }
    }
}